package com.picture.download.util;

import org.nutz.json.Json;
import org.nutz.lang.Strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjidong on 2015/4/22.
 */
public class ResponseUtil {

    public static Map<String, Object> response(int code, String msg, Object payload) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        if (null != msg && !Strings.equals("", msg)) {
            map.put("msg", msg);
        }
        else {
            map.put("msg", "");
        }
        if (payload == null) {
            map.put("payload", "");
        }
        else {
            map.put("payload", Json.toJson(payload));
        }
        return map;
    }

    public static Map<String, Object> response(int code, String msg, List<Object> payload) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        if (null != msg && !Strings.equals("", msg)) {
            map.put("msg", msg);
        }
        else {
            map.put("msg", "");
        }
        if (payload == null) {
            map.put("payload", new ArrayList<>());
        }
        else {
            map.put("payload", Json.toJson(payload));
        }
        return map;
    }

    public static Map<String, Object> failureResponse(int code, String msg, List<Object> payload) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 1);
        if (null != msg && !Strings.equals("", msg)) {
            map.put("msg", msg);
        }
        else {
            map.put("msg", "");
        }
        if (payload == null) {
            map.put("payload", new ArrayList<>());
        }
        else {
            map.put("payload", payload);
        }
        return map;
    }

    public static Map<String, Object> failureResponse(int code, String msg, Object payload) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 1);
        if (null != msg && !Strings.equals("", msg)) {
            map.put("msg", msg);
        }
        else {
            map.put("msg", "");
        }
        if (payload == null) {
            map.put("payload", new ArrayList<>());
        }
        else {
            map.put("payload", payload);
        }
        return map;
    }
}
